package
{
	import com.app.base.Display;
	import com.app.components.Image;
	import com.app.components.Navigator;
	import com.app.components.Scroll;
	import com.app.events.ControlEvent;
	import com.app.system.DeviceInfo;
	import com.app.system.Systems;
	import com.app.utils.Config;
	import com.app.utils.FPS;
	import com.candykingdom.manage.Manage;
	import com.candykingdom.manage.PageNames;
	import com.candykingdom.model.PageModel;
	import com.candykingdom.pages.PageAbout;
	import com.candykingdom.pages.PageActivity;
	import com.candykingdom.pages.PageBulletin;
	import com.candykingdom.pages.PageCollect;
	import com.candykingdom.pages.PageExchange;
	import com.candykingdom.pages.PageFAQ;
	import com.candykingdom.pages.PageFamily;
	import com.candykingdom.pages.PageGift;
	import com.candykingdom.pages.PageIndex;
	import com.candykingdom.pages.PageIntegral;
	import com.candykingdom.pages.PageKingdom;
	import com.candykingdom.pages.PageMall;
	import com.candykingdom.pages.PageMenus;
	import com.candykingdom.pages.PageRecord;
	import com.candykingdom.pages.PageTOP;
	import com.candykingdom.pages.PageUser;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	
	import flash.desktop.NativeApplication;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	import flash.utils.setTimeout;
	
	[SWF(width="640", height="1136", frameRate="30", backgroundColor="#CDE9EB")]
	public class CandyKingdom extends Display
	{
		[Embed(source="assets/Default-Portrait.jpg")]
		private var background_class:Class;
		private var _background:Bitmap;
		
		private const _navigatorTargetX:int = 500;
		private var _navigator:Navigator;
		private var _navigatorScorll:Scroll;
		
		private var _pageMenus:PageMenus;
		private var _pageTOP:PageTOP;
		
		public function CandyKingdom()
		{
			super();
			
			//禁止缩放
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.quality = StageQuality.HIGH;
			//scrollRect = new Rectangle(0 ,0 ,640 ,1136);
			
			//启动画面
			_background = new background_class();
			background_class = null;
			_background.smoothing = true;
			_background.width = stage.fullScreenWidth/640*_background.width;
			_background.height = stage.fullScreenHeight/1136*_background.height;
			_background.x = (stage.fullScreenWidth-_background.width)/2;
			_background.y = (stage.fullScreenHeight-_background.height)/2;
			parent.addChild(_background);
			
			//内部初始完成
			loaderInfo.addEventListener(Event.COMPLETE 
				,function(event:Event):void{stage?init(null):addEventListener(Event.ADDED_TO_STAGE ,init);});
		}
		
		private function init(event:Event):void
		{
			/** 自适应 */
			Config.autoScale(this ,640 ,1136 ,35);
			
			/** 节约内存 */
			NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, function (e:Event):void {visible = true;});
			NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, function (e:Event):void {visible = false;});
			
			/** 操控模式 */
			if(DeviceInfo.platform=="Windows")
			{
				Multitouch.inputMode = MultitouchInputMode.NONE;
			}else{
				Multitouch.inputMode = MultitouchInputMode.NONE;
				//Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
			}
			
			/** FPS RAM  */
			FPS.Start(stage);
			
			
			/** 初始主菜单 */
			_pageMenus = new PageMenus_Class() as PageMenus;
			var scorllMenu:Scroll = new Scroll(new Rectangle(0 ,0 ,640 ,1136) ,new Rectangle(0 ,0 ,_pageMenus.width ,_pageMenus.height) ,Scroll.VERTICAL ,0);
			addChild(scorllMenu);
			scorllMenu.addChild(_pageMenus);
			
			
			
			/** 初始所有页面到导航器 并设置拖导航器拖动组件 */
			_navigator = new Navigator(new Rectangle(0 ,0 ,640 ,1136));
			_navigator.push(PageNames.INDEX ,new PageIndex_Class() as PageIndex);
			_navigator.push(PageNames.KINGDOM ,new PageKingdom_Class() as PageKingdom);
			_navigator.push(PageNames.ACTIVITY ,new PageActivity_Class() as PageActivity);
			_navigator.push(PageNames.MALL ,new PageMall_Class() as PageMall);
			_navigator.push(PageNames.INTEGRAL ,new PageIntegral_Class() as PageIntegral);
			_navigator.push(PageNames.EXCHANGE ,new PageExchange_Class() as PageExchange);
			_navigator.push(PageNames.GIFT ,new PageGift_Class() as PageGift);
			_navigator.push(PageNames.RECORD ,new PageRecord_Class() as PageRecord);
			_navigator.push(PageNames.COLLECT ,new PageCollect_Class() as PageCollect);
			_navigator.push(PageNames.FAMILY ,new PageFamily_Class() as PageFamily);
			_navigator.push(PageNames.BULLETIN ,new PageBulletin_Class() as PageBulletin);
			_navigator.push(PageNames.USER ,new PageUser_Class() as PageUser);
			_navigator.push(PageNames.FAQ ,new PageFAQ_Class() as PageFAQ);
			_navigator.push(PageNames.ABOUT ,new PageAbout_Class() as PageAbout);
			_navigator.setHome(PageNames.INDEX);
			
			_navigatorScorll = new Scroll(new Rectangle(0 ,0 ,640 ,1136) ,new Rectangle(_navigatorTargetX ,0 ,640 ,1136) ,Scroll.HORIZONTAL ,0);
			addChild(_navigatorScorll);
			_navigatorScorll.addChild(_navigator);
			
			
			/** 初始顶部菜单 */
			_pageTOP = new PageTOP_Class() as PageTOP;
			addChild(_pageTOP);
			_pageTOP.y = 35;
			
			
			/** 初始事件 */
			
			Manage.pageModel.addEventListener(PageModel.PAGE_NAME ,gotoPage);
			Manage.pageModel.addEventListener(PageModel.PAGE_HOME ,gotoPage);
			Manage.pageModel.addEventListener(PageModel.PAGE_BACK ,gotoPage);
			
			_navigatorScorll.addEventListener(ControlEvent.DRAG_START ,navigatorDragHandler);
			_navigatorScorll.addEventListener(ControlEvent.DRAG_STOP ,navigatorDragHandler);
			
			_navigator.addEventListener(Event.COMPLETE ,gotoPageComplete);
			
			
			/** 回收处理 */
			if(_background!=null)
			{
				setTimeout(viewMain ,3500);
			}else{
				Systems.gc();
			}
		}
		
		/** 删除启动画面 */
		private function viewMain():void
		{
			Systems.gc();
			parent.removeChild(_background);
			_background = null;
		}
		
		
		
		/**
		 * 导航器拖动处理
		 * @param event
		 * 
		 */	
		private function navigatorDragHandler(event:ControlEvent):void
		{
			switch(event.type)
			{
				case ControlEvent.DRAG_START:
					_navigatorScorll.mouseChildren = false;
					return;
					
				case ControlEvent.DRAG_STOP:
					TweenMax.killTweensOf(_navigatorScorll);
					TweenMax.to(_navigatorScorll ,.25 ,{x:_navigatorScorll.x<_navigatorTargetX/2?0:_navigatorTargetX ,onComplete:navigatorTweenComplete});
					return;
			}
		}
		
		private function navigatorTweenComplete():void
		{
			if(_navigatorScorll.x<=0)
				_navigatorScorll.mouseChildren = true;
		}
		
		
		/**
		 * 换页处理 
		 * @param event
		 * 
		 */		
		private var _eventType:String;
		private function gotoPage(event:Event):void
		{
			switch(event.type)
			{
				case PageModel.PAGE_NAME:
					_navigator.show(Manage.pageModel.pageName);
					break;
					
				case PageModel.PAGE_BACK:
					_navigator.back(_navigatorScorll.x<=0);
					break;
					
				case PageModel.PAGE_HOME:
					_navigator.getHome(_navigatorScorll.x<=0);
					break;
			}
			
			if(_navigatorScorll.x>0)
			{
				TweenMax.killTweensOf(_navigatorScorll);
				TweenMax.to(_navigatorScorll ,.75 ,{x:0 ,onComplete:navigatorTweenComplete});
			}
		}
		
		
		
		/**
		 * 换页完成 
		 * 
		 */		
		private function gotoPageComplete(event:Event):void
		{
			
		}
	}
}